﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Web;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Net.Sockets;
using System.Net;

namespace xunsearch.lib
{
    public class Tool
    {
        public static bool isNumber(string num) 
        {
            try
            {
                int.Parse(num);
                return true;
            }
            catch { 
            }
            return false;
        }
        public static List<string> preg_match_all(string reg, string content)
        {
            List<string> list = new List<string>();
            foreach (Match m in Regex.Matches(content, @reg, RegexOptions.IgnoreCase | RegexOptions.Multiline))
            {
                list.Add(m.Groups[0].Value);
            }
            return list;
        }

        public static List<string> explode(string arg, string content) 
        {
            List<string> list = new List<string>();
            string[] str = content.Split(arg.ToCharArray());
            foreach (string s in str) 
            {
                list.Add(s);
            }
            return list;
        }

        public static IEnumerable<string> preg_split(string pattern, string subject)
        {
            return Regex.Split(subject, pattern);
        }


        public static bool CheckEncode(string srcString)
        {
            int strLen = srcString.Length;

            //字符串的长度，一个字母和汉字都算一个  

            int bytLeng = System.Text.Encoding.UTF8.GetBytes(srcString).Length;

            //字符串的字节数，字母占1位，汉字占2位,注意，一定要UTF8  

            bool chkResult = false;
            if (strLen < bytLeng)

            //如果字符串的长度比字符串的字节数小，当然就是其中有汉字啦^-^  
            {
                chkResult = true;
            }

            return chkResult;
        }

        public static string str_repeat(string s, int n) 
        {
            return String.Concat(Enumerable.Repeat(s, n));
        }
        public static string md5(string str) 
        {
            // 创建MD5类的默认实例：MD5CryptoServiceProvider
            MD5 md5 = MD5.Create();
            byte[] bs = Encoding.UTF8.GetBytes(str);
            byte[] hs = md5.ComputeHash(bs);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in hs)
            {
                // 以十六进制格式格式化
                sb.Append(b.ToString("x2"));
            }
            return sb.ToString();
        }
        public static Dictionary<string, string> unpack(string fm, string str) 
        {
            return new Dictionary<string, string>();
        }

        public static object GetCache(string CacheKey) 
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
        }

        public static void SetCache(string CacheKey, object obj) 
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, obj);
        }

        public static string GetFile(string file) 
        {
            StreamReader objReader = new StreamReader(file);
            string ret = "";
            ret = objReader.ReadToEnd();
            objReader.Close();
            return ret;
        }
        public static Dictionary<string, string> GetConfigValue(string val) 
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            string[] str = val.Split('\n');
            list.Add(str[0], str[1]);
            return list;
        }
        /// <summary>
        /// 字符串转成byte[]
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public static byte[] ToByteArray(string buf)
        {
            byte[] btyes = new byte[buf.Length];
            for (int i = 0; i < buf.Length; i++)
            {
                btyes[i] = (byte)buf[i];
            }
            return btyes;           ;
        }
        /// <summary>
        /// 字节转成字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ByteArrayToString(byte[] bytes) 
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += (char)bytes[i];
                }
            }
            return returnStr; 
        }

        /// <summary>
        /// 转成utf-8字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToUTF8(string str) 
        {
            return ByteArrayToString(Encoding.UTF8.GetBytes(str));
        }

    
        /// <summary>
        /// 字符串转在整数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ToNum(string str)
        {
            int num = 0;
            for (int i = 0; i < str.Length; i++) 
            {
                num += ((int)str[i]) << (4 * i);
            }
            return num;
            
        }
        /// <summary>
        /// 转成C#的utf8格式
        /// </summary>
        /// <param name="hex"></param>
        /// <param name="charset"></param>
        /// <returns></returns>
        public static string UnHex(string hex)
        {
            byte[] s = new byte[hex.Length];
            for (int i = 0; i < hex.Length; i++) 
            {
                s[i] = System.Convert.ToByte(hex[i]);
            }
            string s2 = Encoding.UTF8.GetString(s);
            return s2; 
            
        }
    }
}
